#ifndef __BOARD_BASE_H
#define __BOARD_BASE_H
#ifdef  __cplusplus
    extern "C" {
#endif
/*-----------------------------------------------------------------------
|                               Includes                                |
-----------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include "main.h"
#include "gpio.h"
#include "usart.h"
//#include "FreeRTOS.h"
//#include "queue.h"
//#include "task.h"
#include "tim.h"
#include "dac.h"

/*-----------------------------------------------------------------------
|                                define                                 |
-----------------------------------------------------------------------*/
#define ADC1_BUF_SIZE 4
#define ADC2_BUF_SIZE 5
extern uint16_t ADC1Buf[ADC1_BUF_SIZE];
extern uint16_t ADC2Buf[ADC2_BUF_SIZE];
//*** <<< Use Configuration Wizard in Context Menu >>> ***
//<h> setting

//</h>

//*** <<< end of configuration section >>>    ***
/* fast pin operate */
// Pin input read
#define Pinx_Read(Labelx)	(!!((Labelx##_GPIO_Port->IDR & Labelx##_Pin)))
// Pin output read
#define Pinx_ORead(Labelx)	(!!((Labelx##_GPIO_Port->ODR & Labelx##_Pin)))
// Pin set high
#define Pinx_SET(Labelx)	(Labelx##_GPIO_Port->BSRR = (uint32_t)Labelx##_Pin)
// Pin set low
#define Pinx_RESET(Labelx)	(Labelx##_GPIO_Port->BRR = (uint32_t)Labelx##_Pin)
// Pin write out
#define Pinx_Write(Labelx, state) (((state) != 0) ? Pinx_SET(Labelx) : Pinx_RESET(Labelx))
// Pin togger out
#define Pinx_Togger(Labelx)                                                                   \
        do{                                                                                   \
            uint32_t odr = (Labelx##_GPIO_Port->ODR & Labelx##_Pin);                          \
            Labelx##_GPIO_Port->BSRR = ((odr & Labelx##_Pin) << 16) | (~odr & Labelx##_Pin);  \
        } while (0)

/* board LED ctrl */
// LEDx Control
#if 0 // 0:Low voltage is valid
#define LEDx_Write(LEDx, state) Pinx_Write(LEDx, (state))
#define LEDx_Read(LEDx) (!!Pinx_ORead(LEDx))
#else //
#define LEDx_Write(LEDx, state) Pinx_Write(LEDx, !(state))
#define LEDx_Read(LEDx) (!Pinx_ORead(LEDx))
#endif
#define LEDx_Togger(LEDx) Pinx_Togger(LEDx)
// board KEY Read input state
#define KEYx_Read(KEYx, index) ((Pinx_Read(KEYx)) << index)

typedef struct
{
    int64_t tick;
    int64_t deltaTick;
}bsp_timeMesure_t;


// ADC1
#define DCDC_ADC_T1_ID 0
#define DCDC_ADC_T2_ID 1
#define DCDC_ADC_T3_ID 2
#define DCDC_ADC_CH_IPA_HI_ID 3
// ADC2
#define DCDC_ADC_CH_HV_ID 0
#define DCDC_ADC_CH_HI_ID 1
#define DCDC_ADC_CH_LI_ID 2
#define DCDC_ADC_CH_LV_ID 3
#define DCDC_ADC_CH_IPA_LI_ID 4

#define DCDC_ADC_GET_T1()       ADC1Buf[DCDC_ADC_T1_ID]
#define DCDC_ADC_GET_T2()       ADC1Buf[DCDC_ADC_T2_ID]
#define DCDC_ADC_GET_T3()       ADC1Buf[DCDC_ADC_T3_ID]
#define DCDC_ADC_GET_IPA_HI()   ADC1Buf[DCDC_ADC_CH_IPA_HI_ID]
#define DCDC_ADC_GET_HV()       ADC2Buf[DCDC_ADC_CH_HV_ID]
#define DCDC_ADC_GET_HI()       ADC2Buf[DCDC_ADC_CH_HI_ID]
#define DCDC_ADC_GET_LI()       ADC2Buf[DCDC_ADC_CH_LI_ID]
#define DCDC_ADC_GET_LV()       ADC2Buf[DCDC_ADC_CH_LV_ID]
#define DCDC_ADC_GET_IPA_LI()   ADC2Buf[DCDC_ADC_CH_IPA_LI_ID]

/* 获取高侧电压 */
#define DCDC_GET_HV()       (DCDC_ADC_GET_HV() * 179.22798/1000)
/* 获取高侧电流 */
#define DCDC_GET_HI()       (DCDC_ADC_GET_HI() * 0.003857f - 7.75257f)
/* 获取低侧电压 */
#define DCDC_GET_LV()       (DCDC_ADC_GET_LV() * 25.271201/1000)
/* 获取低侧电流 */
#define DCDC_GET_LI()       (DCDC_ADC_GET_LI() * 0.024849f - 50.37024f)
/* 获取高侧谐振腔电流 */
#define DCDC_GET_IPA_HI()   (DCDC_ADC_GET_IPA_HI() * 1.0f - 0.0f)
/* 获取低侧谐振腔电流 */
#define DCDC_GET_IPA_LI()   (DCDC_ADC_GET_IPA_LI() * 1.0f - 0.0f)

/* 获取温度 */
#define DCDC_GET_T1()         ntc_convert_temp(DCDC_ADC_GET_T1())
#define DCDC_GET_T2()         ntc_convert_temp(DCDC_ADC_GET_T2())
#define DCDC_GET_T3()         ntc_convert_temp(DCDC_ADC_GET_T3())

/* 设置同步整流比较电压 */
#define DCDC_SR_SET_COMPHV(x) HAL_DAC_SetValue(&hdac2, DAC_CHANNEL_1, DAC_ALIGN_12B_R, x)
#define DCDC_SR_SET_COMPLV(x) HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, x)

/*-----------------------------------------------------------------------
|                             API FUNCTION                              |
-----------------------------------------------------------------------*/
#define LED_RUN_Pin 		LED1_Pin
#define LED_RUN_GPIO_Port 	LED1_GPIO_Port
#define LED_STA_Pin 		LED2_Pin
#define LED_STA_GPIO_Port 	LED2_GPIO_Port
#define LED_ERR_Pin 		LED3_Pin
#define LED_ERR_GPIO_Port 	LED3_GPIO_Port


void bsp_MesureTimeStart(uint32_t *timecnt);
uint32_t bsp_MesureTime(uint32_t *old_counter);
void bsp_HPMesureTimeStart(bsp_timeMesure_t *timecnt);
int64_t bsp_HPMesureTime(bsp_timeMesure_t *old_counter);
void board_base_init(void);
//void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
extern uint16_t TaskBuzzerID;
void TaskBuzzer(const void *arg);

void Beep(uint32_t time);
float ntc_convert_temp(uint16_t ADC_value);

#ifdef __cplusplus
}
#endif

#endif
